package cn.stylefeng.guns.modular.business.frequency.cmcompanyorder.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.stylefeng.guns.core.exception.ServiceException;
import cn.stylefeng.guns.core.factory.PageFactory;
import cn.stylefeng.guns.core.pojo.page.PageResult;
import cn.stylefeng.guns.modular.business.frequency.cmcompanyorder.entity.CmCompanyOrder;
import cn.stylefeng.guns.modular.business.frequency.cmcompanyorder.mapper.CmCompanyOrderMapper;
import cn.stylefeng.guns.modular.business.frequency.cmcompanyorder.model.param.CmCompanyOrderParam;
import cn.stylefeng.guns.modular.business.frequency.cmcompanyorder.service.CmCompanyOrderService;
import cn.stylefeng.guns.modular.business.frequency.cmcompanyorder.enums.CmCompanyOrderExceptionEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 通讯录联系人表 服务实现类
 *
 * @author yujie
 * @date 2023/05/09 17:17
 */
@Service
public class CmCompanyOrderServiceImpl extends ServiceImpl<CmCompanyOrderMapper, CmCompanyOrder> implements CmCompanyOrderService {

    @Override
    public PageResult<CmCompanyOrder> page(CmCompanyOrderParam cmCompanyOrderParam) {

        // 构造条件
        LambdaQueryWrapper<CmCompanyOrder> queryWrapper = new LambdaQueryWrapper<>();

        // 查询分页结果
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<CmCompanyOrder> list(CmCompanyOrderParam cmCompanyOrderParam) {

        // 构造条件
        LambdaQueryWrapper<CmCompanyOrder> queryWrapper = new LambdaQueryWrapper<>();

        return this.list(queryWrapper);
    }

    @Override
    public void add(CmCompanyOrderParam cmCompanyOrderParam) {

        // 将dto转为实体
        CmCompanyOrder cmCompanyOrder = new CmCompanyOrder();
        BeanUtil.copyProperties(cmCompanyOrderParam, cmCompanyOrder);

        this.save(cmCompanyOrder);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(CmCompanyOrderParam cmCompanyOrderParam) {
        this.removeById(cmCompanyOrderParam.getId());
    }

    @Override
    public void edit(CmCompanyOrderParam cmCompanyOrderParam) {

        // 根据id查询实体
        CmCompanyOrder cmCompanyOrder = this.queryCmCompanyOrder(cmCompanyOrderParam);

        // 请求参数转化为实体
        BeanUtil.copyProperties(cmCompanyOrderParam, cmCompanyOrder);

        this.updateById(cmCompanyOrder);
    }

    @Override
    public CmCompanyOrder detail(CmCompanyOrderParam cmCompanyOrderParam) {
        return this.queryCmCompanyOrder(cmCompanyOrderParam);
    }



    /**
     * 获取通讯录联系人表
     *
     * @author yujie
     * @since 2023/05/09 17:17
     */
    private CmCompanyOrder queryCmCompanyOrder(CmCompanyOrderParam cmCompanyOrderParam) {
        CmCompanyOrder cmCompanyOrder = this.getById(cmCompanyOrderParam.getId());
        if (ObjectUtil.isEmpty(cmCompanyOrder)) {
            throw new ServiceException(CmCompanyOrderExceptionEnum.NOT_EXISTED);
        }
        return cmCompanyOrder;
    }
}